#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    ,ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    m_timer=new QTimer(this);
    //设置定时器周期，30毫秒刷新一次电量值
    m_timer->start(30);
    connect(m_timer,&QTimer::timeout,this,&Widget::changeValue);
    this->setWindowTitle("电池电量显示");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init()
{
    m_margin=5;
    m_isForward=true;
    m_currentValue=10;
    m_maxValue=100;
    m_minValue=0;
    m_batteryWidth=150;
    m_batteryHeight=60;
}

void Widget::drawBorder(QPainter *painter)
{
    //保存画家的当前状态
    painter->save();
    //设置画笔
    painter->setPen(QPen(Qt::gray,8));
    //无画刷
    painter->setBrush(Qt::NoBrush);
    //电池主体居中
    m_batteryRect=QRectF((this->width()-m_batteryWidth)/2,(this->height()-m_batteryHeight)/2,
                        m_batteryWidth,m_batteryHeight);
    //绘制电池边框
//    painter->drawRoundRect(m_batteryRect,15,15);

    //绘制电池头部
//    painter->setPen(QPen(Qt::gray,12));
//    QLine line(m_batteryRect.topRight().x()+10,m_batteryRect.topRight().y()+10,
//               m_batteryRect.bottomRight().x()+10,m_batteryRect.bottomRight().y()-10);
//    painter->drawLine(line);

    //恢复画家保存的状态
    painter->restore();
}

void Widget::drawBackground(QPainter *painter)
{
    //保存画家的当前状态
    painter->save();
    //确认画刷颜色
    if(m_currentValue<=10)
    {
        //红色
        painter->setBrush(QBrush(QColor(204,38,38)));
    }
    else if(m_currentValue<=30)
    {
        //橙色
        painter->setBrush(QBrush(QColor(190,163,0)));
    }
    else
    {
        //绿色
        painter->setBrush(QBrush(QColor(50,205,51)));
    }

    //将电量转化为宽度
    double w=m_currentValue*(m_batteryRect.width()-(m_margin*2))/100;

    //确认左上角位置
    QPointF topLeft=QPointF(m_batteryRect.topLeft().x()+m_margin,m_batteryRect.topLeft().y()+m_margin);

    //确认右下角位置
    QPointF bottomRight=QPointF(m_batteryRect.topLeft().x()+w+m_margin,m_batteryRect.bottomRight().y()-m_margin);

    //绘制电量背景
    painter->setPen(Qt::NoPen);
    QRectF rectF(topLeft,bottomRight);
    painter->drawRoundRect(rectF,10,10);
    //恢复画家保存的状态
    painter->restore();
}

void Widget::drawBackText(QPainter *painter)
{
    //保存画家的当前状态
    painter->save();
    //字体设置
    QFont font;
    font.setFamily("Microsoft Yahei");
    font.setPixelSize(14);
    painter->setFont(font);
    //电量文字
    QString value=QString::number(m_currentValue)+QString("%");
    //绘制文本
    painter->drawText(m_batteryRect,Qt::AlignCenter,value);
    //恢复画家保存的状态
    painter->restore();
}

void Widget::paintEvent(QPaintEvent *event)
{
    //创建画家对象
    QPainter painter(this);
    //设置抗锯齿，使图像更加清晰
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制电池边框和头部
    drawBorder(&painter);
    //绘制电池背景
    drawBackground(&painter);
    //绘制电池内部电量的文字
    drawBackText(&painter);
}

void Widget::changeValue()
{
    if(m_isForward)
    {
        m_currentValue+=1;
    }
    else
    {
        m_currentValue-=1;
    }

    if(m_currentValue>=100)
    {
        m_currentValue=100;
        m_isForward=false;
    }
    if(m_currentValue<=0)
    {
        m_currentValue=0;
        m_isForward=true;
    }
    update();
}
